package com.crop.back.common.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自定义一个 MybatisPlus 配置类，配置分页插件、乐观锁插件
 * mapper 扫描也可在此写上
 */
@Configuration
@MapperScan(basePackages = {"com.crop.back.mapper"})
public class MyBatisPlusConfig {
    /**
     * MyBatis Plus 插件
     * @return MybatisPlusInterceptor 实例
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 添加乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

//    @Bean
//    public Jackson2ObjectMapperBuilderCustomizer builderCustomizer() {
//        return builder -> {
//            // 所有 Long 类型转换成 String 到前台
//            builder.serializerByType(Long.class, ToStringSerializer.instance);
//        };
//    }
}

